'use strict';
/**
 *给用户添加金豆领取奖励
 */
var vk; // 全局vk实例
// 涉及的表名
const dbName = {
	//test: "vk-test", // 测试表
};

var db = uniCloud.database(); // 全局数据库引用
var _ = db.command; // 数据库操作符
var $ = _.aggregate; // 聚合查询操作符
/**
 * 权限注意：访问以下链接查看
 * 文档地址：https://vkdoc.fsq.pub/client/uniCloud/cloudfunctions/cloudObject.html#内置权限
 */
var cloudObject = {
	isCloudObject: true, // 标记为云对象模式
	/**
	 * 请求前处理，主要用于调用方法之前进行预处理，一般用于拦截器、统一的身份验证、参数校验、定义全局对象等。
	 * 文档地址：https://vkdoc.fsq.pub/client/uniCloud/cloudfunctions/cloudObject.html#before-预处理
	 */
	_before: async function() {
		vk = this.vk; // 将vk定义为全局对象
		// let { customUtil, uniID, config, pubFun } = this.getUtil(); // 获取工具包
	},
	/**
	 * 请求后处理，主要用于处理本次调用方法的返回结果或者抛出的错误
	 * 文档地址：https://vkdoc.fsq.pub/client/uniCloud/cloudfunctions/cloudObject.html#after-后处理
	 */
	_after: async function(options) {
		let {
			err,
			res
		} = options;
		if (err) {
			return; // 如果方法抛出错误，直接return;不处理
		}
		return res;
	},
	/**
	 * 获取列表
	 * @url client/user/kh/claim_rewards.getList 前端调用的url参数地址
	 * 添加桌面
	 */
	getList: async function(data) {
		let res = {
			code: 0,
			msg: ''
		};
		let {
			uid
		} = this.getClientInfo(); // 获取客户端信息
		let userInfo = await this.getUserInfo(); // 获取当前登录的用户信息
		// 业务逻辑开始-----------------------------------------------------------
		// 检查对象是否具有指定属性
		if (userInfo.finish.tjzm == false) {
			//添加金豆收支明细
			const addBeanDetail_libao = await db.collection('a_jindou_detail').add({
				user_id: userInfo._id,
				nickname: userInfo.nickname,
				change_num: 50, //变化的金豆数量
				change_reason: "添加小程序到微信桌面奖励50金豆",
				type: "income", //类型：expend:支出,income:收入。
				appid: userInfo.appid,
				appname: userInfo.appname,
				tfid: userInfo.tfid,
				invite_code: userInfo.invite_code,
				middleman_id: userInfo.middleman_id,
				province: userInfo.province, //省份
				city: userInfo.city, //市
				_add_time: new Date().getTime()
			})
			//给用户增加金豆
			const updateUser_qiandao = await db.collection('uni-id-users').where({
				_id: userInfo._id
			}).update({
				balance: _.inc(50),
				finish: {
					tjzm: true
				}
			})
		}
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	},
	/**
	 * 模板函数
	 * @url client/user/kh/claim_rewards.test 前端调用的url参数地址
	 */
	kefu: async function(data) {
		let res = {
			code: 0,
			msg: ''
		};
		let {
			uid
		} = this.getClientInfo(); // 获取客户端信息
		let userInfo = await this.getUserInfo(); // 获取当前登录的用户信息
		// 业务逻辑开始-----------------------------------------------------------
		// 检查对象是否具有指定属性
		if (userInfo.finish.tjkf == false) {
			//添加金豆收支明细
			const addBeanDetail_libao = await db.collection('a_jindou_detail').add({
				user_id: userInfo._id,
				nickname: userInfo.nickname,
				change_num: 50, //变化的金豆数量
				change_reason: "添加客服微信奖励50金豆",
				type: "income", //类型：expend:支出,income:收入。
				appid: userInfo.appid,
				appname: userInfo.appname,
				tfid: userInfo.tfid,
				invite_code: userInfo.invite_code,
				middleman_id: userInfo.middleman_id,
				province: userInfo.province, //省份
				city: userInfo.city, //市
				_add_time: new Date().getTime()
			})
			//给用户增加金豆
			const updateUser_qiandao = await db.collection('uni-id-users').where({
				_id: userInfo._id
			}).update({
				balance: _.inc(50),
				finish: {
					tjkf: true
				}
			})
		}
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	},
	/**
	 * 模板函数
	 * @url client/user/kh/claim_rewards.test 前端调用的url参数地址
	 * 分享
	 * 
	 */
	fx: async function(data) {
		let res = {
			code: 0,
			msg: ''
		};
		let {
			uid
		} = this.getClientInfo(); // 获取客户端信息
		let userInfo = await this.getUserInfo(); // 获取当前登录的用户信息
		// 业务逻辑开始-----------------------------------------------------------
		// 检查对象是否具有指定属性
		if (userInfo.finish.fx == false) {
			//添加金豆收支明细
			const addBeanDetail_libao = await db.collection('a_jindou_detail').add({
				user_id: userInfo._id,
				nickname: userInfo.nickname,
				change_num: 50, //变化的金豆数量
				change_reason: "分享奖励50金豆",
				type: "income", //类型：expend:支出,income:收入。
				appid: userInfo.appid,
				appname: userInfo.appname,
				tfid: userInfo.tfid,
				invite_code: userInfo.invite_code,
				middleman_id: userInfo.middleman_id,
				province: userInfo.province, //省份
				city: userInfo.city, //市
				_add_time: new Date().getTime()
			})
			//给用户增加金豆
			const updateUser_qiandao = await db.collection('uni-id-users').where({
				_id: userInfo._id
			}).update({
				balance: _.inc(50),
				finish: {
					fx: true
				}
			})
		}

		// 业务逻辑结束-----------------------------------------------------------
		return res;
	},
	/**
	 * 模板函数
	 * @url client/user/kh/claim_rewards.test 前端调用的url参数地址
	 * 招商代理
	 */
	zsdl: async function(data) {
		let res = {
			code: 0,
			msg: ''
		};
		let {
			uid
		} = this.getClientInfo(); // 获取客户端信息
		let userInfo = await this.getUserInfo(); // 获取当前登录的用户信息
		// 业务逻辑开始-----------------------------------------------------------
		if (userInfo.finish.zsdl == false) {
			//添加金豆收支明细
			const addBeanDetail_libao = await db.collection('a_jindou_detail').add({
				user_id: userInfo._id,
				nickname: userInfo.nickname,
				change_num: 50, //变化的金豆数量
				change_reason: "添加客服微信奖励50金豆",
				type: "income", //类型：expend:支出,income:收入。
				appid: userInfo.appid,
				appname: userInfo.appname,
				tfid: userInfo.tfid,
				invite_code: userInfo.invite_code,
				middleman_id: userInfo.middleman_id,
				province: userInfo.province, //省份
				city: userInfo.city, //市
				_add_time: new Date().getTime()
			})
			//给用户增加金豆
			const updateUser_qiandao = await db.collection('uni-id-users').where({
				_id: userInfo._id
			}).update({
				balance: _.inc(50),
				finish: {
					zsdl: true
				}
			})
		}

		// 业务逻辑结束-----------------------------------------------------------
		return res;
	},
	/**
	 * 签到再分享领取金豆
	 */
	qdzfx: async function(data) {
		let res = {
			code: 0,
			msg: ''
		};
		let {
			uid
		} = this.getClientInfo(); // 获取客户端信息
		let userInfo = await this.getUserInfo(); // 获取当前登录的用户信息
		if (userInfo.finish.zsdl == false) {
			//添加金豆收支明细
			const addBeanDetail_libao = await db.collection('a_jindou_detail').add({
				user_id: userInfo._id,
				nickname: userInfo.nickname,
				change_num: 70, //变化的金豆数量
				change_reason: "签到之后再领取70金豆",
				type: "income", //类型：expend:支出,income:收入。
				appid: userInfo.appid,
				appname: userInfo.appname,
				tfid: userInfo.tfid,
				invite_code: userInfo.invite_code,
				middleman_id: userInfo.middleman_id,
				province: userInfo.province, //省份
				city: userInfo.city, //市
				_add_time: new Date().getTime()
			})
			//给用户增加金豆
			const updateUser_qiandao = await db.collection('uni-id-users').where({
				_id: userInfo._id
			}).update({
				balance: _.inc(70),
			})
		}

	},
	/**
	 * 连续签到
	 */
	lxqd: async function(data) {
		let res = {
			code: 0,
			msg: ''
		};
		let {
			uid
		} = this.getClientInfo(); // 获取客户端信息
		let userInfo = await this.getUserInfo(); // 获取当前登录的用户信息
		let {
			signin_reward,
			sign_datas
		} = data;
		console.log("signin_reward=", signin_reward);
		//如果没传签到奖励金额，则默认30
		if (!signin_reward) {
			signin_reward = 30
		}
		if(sign_datas>7){
			sign_datas=sign_datas%7
		}
		console.log("触发签到事件");
		//添加金豆收支明细
		const addBeanDetail_qiandao = await db.collection('a_jindou_detail').add({
			user_id: userInfo._id,
			nickname: userInfo.nickname || "匿名用户",
			change_num: signin_reward, //扣除的金豆数量
			change_reason: "签到奖励" + signin_reward + "金豆",
			type: "income", //类型：expend:支出,income:收入。
			appid: userInfo.appid,
			appname: userInfo.appname,
			tfid: userInfo.tfid,
			invite_code: userInfo.invite_code,
			middleman_id: userInfo.middleman_id,
			province: userInfo.province, //省份
			city: userInfo.city, //市
			_add_time: new Date().getTime()
		})
		//添加签到记录
		const addsign_qiandao = await db.collection('a_sign').add({
			user_id: userInfo._id,
			nickname: userInfo.nickname,
			reward: "金豆",
			reward_content: signin_reward + "金豆",
			reward_num: signin_reward,
			appid: userInfo.appid,
			appname: userInfo.appname,
			tfid: userInfo.tfid,
			invite_code: userInfo.invite_code,
			middleman_id: userInfo.middleman_id,
			province: userInfo.province, //省份
			city: userInfo.city, //市
			sign_datas: sign_datas + 1, //连续签到天数
			_add_time: new Date().getTime()
		})

		//给用户增加金豆
		const updateUser_qiandao = await db.collection('uni-id-users').where({
			_id: userInfo._id
		}).update({
			balance: _.inc(signin_reward),
			finish: {
				xinren: true
			}
		})
	},
};

module.exports = cloudObject;